
package de.dtools.n2g.core;

/**
 *	@project:	note2go
 *	@author:	D.Toepel
 *	@version:	2009-08-04
 *	@note:		---
 *	
 *	Copyright (C) 2009  D.Toepel <de.dtools> {at} <gmail> {dot} <com>
 *	
 *	This program is free software: you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation, either version 3 of the License, or
 *	(at your option) any later version.
 *	
 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *	
 *	You should have received a copy of the GNU General Public License
 *	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *	
 */

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Component;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.text.DateFormat;
import java.util.Date;

import javax.swing.*;

import de.dtools.n2g.res.Resource;

public class Result extends JPanel{
	
	private static final long serialVersionUID = 1L;
	private ResultHeader resultHeader;
	private JPanel resultGrid;
	private JPanel resultNavi;
	private JLabel resultLabel;
	private String currentPage;
	private String lastPage;
	private JButton btnNaviBack;
	private JButton btnNaviNext;
	private JLabel labelItems;
	private JComboBox fieldItems;
	private JButton btnItems;
	private Resource res = new Resource();
	private JScrollPane resultScroll;
	private int xWidth;
	private JButton btnShow;
	private String actionCommand;
	
	public Result(){
		super();
	}
	
	public void setSearchResult(ResultHeader resultHeader,int xWidth, JButton btnShow){
		this.resultHeader = resultHeader;
		this.xWidth = xWidth;
		this.btnShow = btnShow;
		initGUI();
		this.setVisible(true);
	}
	
	private void initGUI(){
		try {
			
			this.setLayout(new BorderLayout());
			this.setPreferredSize(new java.awt.Dimension(400, 459));
			this.setBackground(new java.awt.Color(214,217,223));
			{
				resultScroll = new JScrollPane();
				resultScroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
				resultScroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
				resultScroll.setViewportView(getResultGrid());
			}
    		this.add(getResultNavi(), BorderLayout.NORTH);
    		this.add(resultScroll, BorderLayout.CENTER);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	private JPanel getResultNavi(){
		resultNavi = new JPanel();
		{
			resultLabel = new JLabel(
					"Suchergebnis : " + currentPage +
					" / " + lastPage + "   ");
			resultLabel.setHorizontalAlignment(SwingConstants.LEFT);
			
		}
		btnNaviBack = new JButton(res.getLeftArrowIcon());
		{
			btnNaviBack.setMargin(new Insets(1,1,1,1));
			btnNaviBack.setBorder(BorderFactory.createEmptyBorder(2, 5, 2, 5));
		}
		btnNaviNext = new JButton(res.getRightArrowIcon());
		{
			btnNaviNext.setMargin(new Insets(1,1,1,1));
			btnNaviNext.setBorder(BorderFactory.createEmptyBorder(2, 5, 2, 5));
		}
		labelItems = new JLabel("  Ergebnisse pro Seite : ");
		fieldItems = new JComboBox(new String[] {"10","20","50"});
		{
			fieldItems.setEditable(false);
			fieldItems.addItemListener(new ItemListener(){
				public void itemStateChanged(ItemEvent evt){
//					CardLayout cl = (CardLayout)(cards.getLayout());
//					cl.show(cards, (String)evt.getItem());
				}
			});
		}
		
		resultNavi.add(resultLabel);
		resultNavi.add(btnNaviBack);
		resultNavi.add(btnNaviNext);
		resultNavi.add(labelItems);
		resultNavi.add(fieldItems);
		
		return resultNavi;
	}

	private JPanel getResultGrid(){
		GridBagConstraints c = new GridBagConstraints();
		
		Date d = new Date();  
		DateFormat df;  
		df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM );
		
		
		resultGrid = new JPanel(new GridBagLayout());
		resultGrid.setBackground(new java.awt.Color(230,230,230));
		{        	
			String titelStr = "Titel : " + df.format(d) + " df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM );";
			JLabel titel,jBlank;
			JTextPane note;
	        JButton editBtn;
	        
	        for (int i = 0; i < 20; i=i+4)
			{
	        	c.ipady = 2;
	        	
	        	titel = new JLabel(getSubString(titelStr));
	        	titel.setForeground(new java.awt.Color(92,108,129));
	        	titel.setMaximumSize(new Dimension(320,28));
	        	titel.setFont(new java.awt.Font("Verdana",1,12));
	        	{
		        	c.weightx = 0.5;
		        	c.fill = GridBagConstraints.BOTH;
		        	c.gridx = 0;
		        	c.gridwidth = 2;
		        	c.gridy = i;
		        	resultGrid.add(titel, c);
	        	}
	        	editBtn = new JButton("Anzeigen");
	        	editBtn.setActionCommand(""+i);
	        	editBtn.setAlignmentX(Component.RIGHT_ALIGNMENT);
	        	editBtn.setSize(100, 30);
	        	editBtn.addActionListener(new ActionListener() {
	            	public void actionPerformed(ActionEvent evt) {
	            		actionCommand = evt.getActionCommand();
	            		showNote();
	    			}
	    		});
	        	{
		        	c.fill = GridBagConstraints.NONE;
		        	c.weightx = 0.0;
		        	c.gridx = 2;
		        	c.gridy = i;
		        	resultGrid.add(editBtn, c);
	        	}        	
	        	note = new JTextPane();
	        	note.setText("Bezeichnung: \nNr.:"+i);
	        	note.setEditable(false);
	        	{
		        	c.fill = GridBagConstraints.BOTH;
		        	c.weightx = 0.5;
		        	c.gridx = 0;
		        	c.gridwidth = 4;
		        	c.gridy = i+1;
		        	resultGrid.add(note, c);
	        	}
		    	jBlank = new JLabel();
		    	jBlank.setBackground(new java.awt.Color(65,65,65));
		    	jBlank.setOpaque(true);
		    	{
		        	c.fill = GridBagConstraints.BOTH;
		        	c.weightx = 0;
		        	c.gridx = 0;
		        	c.gridwidth = 4;
		        	c.gridy = i+2;
		        	resultGrid.add(jBlank, c);
		    	}
			}
		}
		
        
        
        
        JLabel jFill = new JLabel();
    	c.fill = GridBagConstraints.BOTH;
    	c.weightx = 0.0;
    	c.weighty = 1.0;
    	c.gridwidth = 3;
    	c.gridx = 0;
    	c.gridy = GridBagConstraints.PAGE_END;
    	resultGrid.add(jFill, c);

    	return resultGrid;
	}

	private String getSubString(String titelStr) {
		int maxChar = Math.round((xWidth-100) / 8);
		if (maxChar < titelStr.length())
			titelStr = titelStr.substring(0, maxChar) + " ...";
		System.out.println(""+xWidth);
		return titelStr;
	}
	
	private void showNote(){
		Runnable r = new Runnable(){
			public void run() {
				btnShow.setActionCommand(actionCommand);
				btnShow.doClick();
			}	
		};
		SwingUtilities.invokeLater(r);
	}
}
